local anming = fk.CreateSkill {
    name = "SP_heg__anming",
}

Fk:loadTranslationTable {
    ["SP_heg__anming"] = "安民",
    [":SP_heg__anming"] = "出牌阶段限一次，你展示至多X张手牌并选择两名角色，每名角色轮流选择获得一张牌，若这两名角色：1.体力值相等，你回复1点体力并复原武将牌；" ..
    "2.手牌数相等，你可以弃置其中一名角色区域内的一张牌，然后若其与你势力相同，你与其<a href='heg_swap_deputy'>交换副将</a>（X为你的体力值）。",

    ["#SP_heg__anming"] = "安民：选择至多体力值张手牌和两名角色",
    ["#SP_heg__anming-choose"] = "安民：你可以弃置一名角色区域内的一张牌，然后若其与你势力相同，你与其交换副将",

    ["$SP_heg__anming1"] = "粮草事关重大，且容我仔细计较。",
    ["$SP_heg__anming2"] = "粮草之事，且不宜迟。",
}

local H = require "packages.ol_hegemony.util"

anming:addEffect("active", {
    anim_type = "support",
    prompt = "#SP_heg__anming",
    can_use = function(self, player)
        return player:usedSkillTimes(anming.name, Player.HistoryPhase) == 0 and not player:isKongcheng()
    end,
    min_card_num = 1,
    max_card_num = function(self, player)
        return player.hp
    end,
    target_num = 2,
    card_filter = function(self, player, to_select, selected, selected_targets)
        return #selected <= player.hp and not player:prohibitDiscard(to_select)
    end,
    target_filter = function(self, player, to_select, selected)
        if #selected < 2 then
            if #selected == 0 then
                return true
            else
                return to_select ~= selected[1]
            end
        end
    end,
    on_use = function(self, room, effect)
        local from = effect.from
        local to1 = effect.tos[1]
        local to2 = effect.tos[2]
        local cards = effect.cards
        room:moveCards({
            ids = cards,
            toArea = Card.Processing,
            moveReason = fk.ReasonJustMove,
            proposer = from,
            from = from,
        })
        table.forEach(room:getAlivePlayers(), function(p) room:fillAG(p, cards) end)
        room:sortByAction(effect.tos)
        local x = 0
        local n = #cards
        while x < n do
            for _, to in ipairs(effect.tos) do
                if not to.dead then
                    local chosen = room:askToAG(to, {
                        id_list = cards,
                        cancelable = false,
                        skill_name = anming.name,
                    })
                    room:moveCards {
                        ids = { chosen },
                        to = to,
                        toArea = Player.Hand,
                        moveReason = fk.ReasonPut,
                    }
                    room:takeAG(to, chosen, room.players)
                    table.removeOne(cards, chosen)
                    x = x + 1
                    if x >= n then
                        break
                    end
                end
            end
            if not table.find(effect.tos, function(p) return not p.dead end) then
                break
            end
        end
        table.forEach(room:getAlivePlayers(), function(p) room:closeAG(p) end)
        local toDiscard = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
        if #toDiscard > 0 then
        room:moveCards{
           ids = toDiscard,
           toArea = Card.DiscardPile,
           moveReason = fk.ReasonPutIntoDiscardPile
        }
       end
        --体力都相等
        if to1.hp == to2.hp then
            if from:isWounded() then
                room:recover({
                    who = from,
                    num = 1,
                    recoverBy = from,
                    skillName = anming.name,
                })
                from:reset()
            end
        end
        --手牌数相等
        if to1:getHandcardNum() == to2:getHandcardNum() then
            local to = room:askToChoosePlayers(from,{
                targets = effect.tos,
                min_num = 1,
                max_num = 1,
                prompt = "#SP_heg__anming-choose",
                skill_name = anming.name,
                cancelable = true,
            })[1]
            if to then
                local card = room:askToChooseCard(from,{
                    target = to,
                    flag = "hej",
                    skill_name = anming.name,
                })
                if card then
                    room:throwCard(card, anming.name, to, from)
                    if H.compareKingdomWith(to, from) then
                    H.SwapTwoPlayersDeputy(room, from, to)
                    end
                end
            end
        end
    end,
})

return anming
